package com.aletto.general_store.ui.popup;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.aletto.general_store.R;
import com.aletto.general_store.data.DataRepository;
import com.aletto.general_store.presenters.core.SimpleTemplate;
import com.aletto.general_store.ui.adapters.BrowserHistoryAdapter;
import com.aletto.general_store.utils.event.FavoritesEvent;
import com.aletto.general_store.utils.event.FinishEvent;
import com.lxj.xpopup.core.CenterPopupView;
import com.sisyphus.spinach.core.INetTask;
import com.sisyphus.spinach.network.presenters.NetSingleTask;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

public class ConfirmFavoritesPopup extends CenterPopupView {
//    private RecyclerView recyclerView;

    private int type;

    public ConfirmFavoritesPopup(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.popup_confirm_history;
    }

    public void setType(int type){
        this.type = type;
    }

    @Override
    protected void onCreate() {
        super.onCreate();

        findViewById(R.id.button_cancel).setOnClickListener(v -> dismiss());

        findViewById(R.id.button_confirm).setOnClickListener(v -> provider().execute());
    }

    private SimpleTemplate<Object> provider(){
        return new SimpleTemplate<>() {
            @Override
            public INetTask.View<Object> view() {
                return new INetTask.View<>() {
                    @Override
                    public void success(Object data) {
                        dismiss();
                        EventBus.getDefault().post(new FavoritesEvent(type));
                    }

                    @Override
                    public void complete() {
                        dismiss();
                    }
                };
            }

            @Override
            public INetTask.Presenter<Object, INetTask.View<Object>> presenter() {
                if (type == 0){
                    // 1商品 2店铺
                    return new NetSingleTask<>(DataRepository.getInstance().clearFavorites(1));
                }

                return new NetSingleTask<>(DataRepository.getInstance().clearFavorites(2));
            }

        };
    }


}
